import{createRouter,createWebHashHistory, RouterView} from 'vue-router'
import { h } from 'vue'
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs'
import community from './community'

const router=createRouter({
    history:createWebHashHistory(),
    routes:[
      { path: '/', redirect:'/login' },
      //登录
      {
        path:'/login',
        component:()=>import('@/views/login/login.vue'),
      },
      //首页
      {
        path:'/home',
        meta:{
          title:'首页',
          keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        // meta:{show:true},
        redirect:'/',
        children:[          
          {
          path:'',
          component:()=>import('@/views/home/index.vue')
        },
      ]
      },
      // 增删改查
      {
        path:'/crud',
        meta:{
        title:'增删改查',
        keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        redirect:'/',
        children:[
          {
            path:'',
            component:()=>import('@/views/user/upload.vue')
          }
        ]
      },
      //css样式
      {
        path:'/cssStyle',
        meta:{
        title:'css样式',
        keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        redirect:'/',
        children:[
          {
            path:'',
            component:()=>import('@/views/layout/layout.vue')
          },
        ]
      },
      //测试
      {
        path:'/test',
        meta:{
        title:'测试',
        keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        redirect:'/',
        children:[
          {
            path:'',
            // component:()=>import('@/views/test/b.vue')
            component:()=>import('@/views/test/test.vue')
          },
        ]
      },
      //论坛
      {
        path:'/forum',
        meta:{
        title:'论坛',
        keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        redirect:'/',
        children:[
          {
            path:'',
            component:()=>import('@/views/forum/forum.vue')
          },
        ]
      },
      //留言
      {
        path:'/note',
        meta:{
        title:'留言',
        keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        redirect:'/',
        children:[
          {
            path:'',
            component:()=>import('@/views/note/note.vue')
          },
        ]
      },
          
      // 社区
      {
        path:'/community',
        meta:{
        title:'社区',
        keepAlive: true
        },
        component:()=>import('@/layout/home.vue'),
        redirect:'/',
        children:[
          ...community
        ]
      },
    ],
})

// // 打印子路由路径
export const handleRouteChange = (to,index) => {
  let counterPage=""
  const pathSegments = to.path.split('/').filter(segment => segment.length > 0);
      if (pathSegments.length > 0) {
        // console.log(counterPage.value, 'First segment after /:', pathSegments[0]);
        counterPage= pathSegments[index];
        // console.log('/', pathSegments[0]);
      } else {
        console.log('No segments found after /');
      }
      return counterPage
    };

// 获取首页中的所有子路由，并去除 /home/ 前缀
export const getHomeRoutesPathsAndTitles = async () => {
  const paths = [];
  const titles = [];

  function findHomeChildren(routes) {
    routes.forEach(route => {
        if (route.meta && route.meta.title) {
          paths.push(route.path);
          titles.push(route.meta.title);
        }
      // }
      if (route.children && route.children.length > 0) {
        findHomeChildren(route.children);
      }
    });
  }

  // 确保路由已加载完成
  // await router.isReady();
  findHomeChildren(router.getRoutes());
  
  return { paths, titles };
};

export const getMenuList=async()=>{
  const menuList=[]
  const seenPaths = new Set(); // 用于记录已添加的路径

  router.getRoutes().forEach(route => {
    // 递归函数用于处理子路由
    const processRoute = (route) => {
      if (route.meta && route.meta.title) {
        const path = route.path;
        const name = path.replace('/', ''); // 去掉 / 前缀作为 name
        const title = route.meta.title;
        if (!seenPaths.has(path)) {
          menuList.push({ path, name, title });
          seenPaths.add(path);
        }
      }
      if (route.children && route.children.length) {
        route.children.forEach(childRoute => processRoute(childRoute));
      }
    };

    processRoute(route);
  });
  // console.log(menuList)
  return menuList
}

// 示例调用
// getHomeRoutesPathsAndTitles().then(({ paths, titles }) => {
//   console.log('Paths:', paths);
//   console.log('Titles:', titles);
// });

export default router